﻿using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Platform;
using MiniFox.Platform.Applications.Models;
using MiniFox.Platform.Applications.Providers;
using Newtonsoft.Json.Linq;
using System.Globalization;

namespace Platform.Data
{
    [AutoResolve]
    public class OrganizationProvider : EntityDataProvider<PlatformDbContext>, IOrganizationProvider
    {

        #region org
        private IQueryable<Organization> QueryOrganizations()
        {
            return (from org in this.CurrentDbContext.SysOrganization
                    select new Organization
                    {
                        ID = org.OrgId,
                        Name = org.OrgName,
                        DisplayName = org.DisplayName,
                        Description = org.Description,
                        Icon = org.Icon,
                        //DefaultTheme = org.DefaultTheme,
                        Logo = org.Logo
                    });
        }
        /// <summary>
        /// 获取所有机构（超管功能）
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Organization> GetOrganizations()
        {
            var list = QueryOrganizations().ToArray();
            return list;
        }
        /// <summary>
        /// 获取用户所在的机构
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IEnumerable<Organization> GetOrganizations(string userId)
        {
            var array = (from d in this.CurrentDbContext.SysDept
                         join ud in this.CurrentDbContext.SysUserDept on d.DeptPath equals ud.DeptPath
                         where ud.UserId == userId && d.Enabled == true
                         select d.OrgId).Distinct();
            return QueryOrganizations().Where(a => array.Contains(a.ID));
        }

        #endregion

        #region dept

        private IQueryable<Dept> QueryDepts()
        {
            return (from d in this.CurrentDbContext.SysDept
                    where d.Enabled == true
                    select new Dept
                    {
                        OrganizationID = d.OrgId,
                        DeptID = d.DeptId,
                        DeptName = d.DeptName,
                        DeptPath = d.DeptPath,
                        Description = d.Description,
                        Director = d.Director,
                        SuperID = d.ParentId,
                        Ordinal = d.Ordinal ?? 0,
                        MemberCount = this.CurrentDbContext.SysUserDept.Count(ud => ud.DeptPath.StartsWith(d.DeptPath))
                    });
        }
        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Dept> GetDepts()
        {
            return QueryDepts().ToArray();
        }
        /// <summary>
        /// 获取用户在指定机构中所在的部门
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public IEnumerable<string> GetUserDepts(string userId)
        {
            return (from ud in this.CurrentDbContext.SysUserDept
                    where ud.UserId == userId
                    select ud.DeptPath).ToArray();
        }

        ///// <summary>
        ///// 获取用户在指定机构中所管理的部门及子部门
        ///// </summary>
        ///// <param name="orgId"></param>
        ///// <param name="user"></param>
        ///// <returns></returns>
        //public IEnumerable<Dept> GetManagedDept(SystemUser user)
        //{
        //    var allDepts = QueryDepts();
        //    var userDepts = (from d in allDepts
        //            where d.Director == user.UserID
        //            select d.DeptPath).ToArray();
        //    var depts = new List<Dept>();
        //    userDepts.ForEach(ud => 
        //    {
        //        depts.AddRange(allDepts.Where(d=>d.DeptPath.StartsWith(ud)));
        //    });
        //    return depts;
        //}

        #endregion

    }
}
